﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using DevMaker.SIC.Model;
using log4net;


namespace DevMaker.SIC.Dao {
    public class ProductoDao {

        private static readonly ILog Log =
            LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public List<Producto> ObtieneTodos() {
            var list = new List<Producto>();
            var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SIC_Connection"].ToString());
            try {
                conn.Open();
                var command = new SqlCommand("SP_Productos_L", conn);
                command.CommandType = CommandType.StoredProcedure;
                var reader = command.ExecuteReader();
                while (reader.Read()) {
                    var p = new Producto();
                    p.ReadFromDataReader(reader);
                    list.Add(p);
                }
            } catch (Exception ex) {
                Console.Write(ex.InnerException);
            } finally {
                conn.Close();
            }
            return list;
        }

        public Hashtable ObtieneUltimasCantidades(PuntoVenta puntoVenta) {
            var ht = new Hashtable();
            const string storeName = "sp_cantidad_ultimo_cierre_por_punto_venta";
            var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SIC_Connection"].ToString());

            try {
                conn.Open();
                var command = new SqlCommand(storeName, conn) { CommandType = CommandType.StoredProcedure };
                command.Parameters.AddWithValue("@id_punto", puntoVenta.Code);
                var reader = command.ExecuteReader();
                while (reader.Read()) {
                    var idProducto = reader["id_producto"];
                    if (!ht.Contains(idProducto))
                        ht.Add(idProducto.ToString(), Convert.ToInt64(reader["cantidad_real"]));
                }
            } catch (Exception ex) {
                Log.Error(ex);
                throw;
            } finally {
                if (conn.State != ConnectionState.Closed)
                    conn.Close();
            }
            return ht;
        }

        public List<Producto> ObtieneTopVentas() {
            var list = new List<Producto>();
            var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SIC_Connection"].ToString());
            try {
                conn.Open();
                var command = new SqlCommand("sp_top_10_mas_vendido", conn);
                command.CommandType = CommandType.StoredProcedure;
                var reader = command.ExecuteReader();
                while (reader.Read()) {
                    var p = new Producto();
                    p.ReadFromDataReader(reader);
                    list.Add(p);
                }
            } catch (Exception ex) {
                Console.Write(ex.InnerException);
            } finally {
                conn.Close();
            }
            return list;
        }
    }
}
